//
//  DHRAdioListView.m
//  Moment
//
//  Created by dllo on 15/12/26.
//  Copyright © 2015年 片刻. All rights reserved.
//

#import "DHRAdioListView.h"

@implementation DHRAdioListView
-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        
        NSLog(@"111111*****");
        [self createView];
    }
    return self;
}
-(void)createView{
    _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - (SCREEN_HEIGHT * 0.22)) style:UITableViewStylePlain];
    [self addSubview:_myTableView];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [_myTableView registerClass:[DHRadioListTableViewCell class] forCellReuseIdentifier:@"DHRadioListTableViewCell"];
    _greenView = [[UIView alloc]init];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changList:) name:@"sendRadioArray" object:nil];

}

-(void)changList:(NSNotification *)notification{
    NSDictionary * dic = [NSDictionary dictionaryWithDictionary:[notification object]];
    _radioListArr = [dic objectForKey:@"Array"];
    for (Moreting *moreting in _radioListArr) {
        NSLog(@"moreting == %@",moreting.title);
    }    _figure = [[dic objectForKey:@"mark"] integerValue];
    _greenView.frame = CGRectMake(0, 50*HEIGHT * _figure, 50 * HEIGHT * 0.1, 50*HEIGHT);
    _mark = 0;
    [_myTableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _radioListArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *indetifier = @"DHRadioListTableViewCell";
    DHRadioListTableViewCell *listCell = [tableView dequeueReusableCellWithIdentifier:indetifier];
    listCell.selectionStyle = UITableViewCellAccessoryNone;
    if (_mark == 1) {
        DHSubRadio *radio = [[DHSubRadio alloc]init];
        radio = _radioListArr[indexPath.row];
        listCell.radio = radio;
    }else{
        Moreting *moreting = [[Moreting alloc]init];
        moreting = _radioListArr[indexPath.row];
        listCell.moreting = moreting;
    }
        return listCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50 * HEIGHT;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _greenView.frame = CGRectMake(0, 50 * HEIGHT * indexPath.row, 50 * HEIGHT * 0.1, 50 * HEIGHT);
    if (_mark == 1) {
        DHSubRadio *radio = [[DHSubRadio alloc]init];
        radio = _radioListArr[indexPath.row];
        if (![[[NSUserDefaults standardUserDefaults]objectForKey:@"ID"]isEqualToString:radio.tingid]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"changRadioFirst" object:
             radio];
        }
       //
        
    }else{
    Moreting *moreting = [[Moreting alloc]init];
    moreting = _radioListArr [indexPath.row];
    moreting.figure = indexPath.row;
        if (![[[NSUserDefaults standardUserDefaults]objectForKey:@"ID"]isEqualToString:moreting.tingid]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"changRadioFirst" object:
             moreting];

        }
    }
}
-(void)setFigure:(NSInteger)figure{
    if (_figure != figure) {
        _figure = figure;
    }
   _greenView.frame = CGRectMake(0, 50*HEIGHT * _figure, 50 * HEIGHT * 0.1, 50*HEIGHT);

    CGPoint position = CGPointMake(0, _figure * 50*HEIGHT);
    [_myTableView setContentOffset:position animated:YES];
}
-(void)setRadioListArr:(NSMutableArray *)radioListArr{
    if (_radioListArr != radioListArr) {
        _radioListArr = radioListArr;
    }
    [_myTableView reloadData];
    _greenView.frame = CGRectMake(0, 50*HEIGHT * _figure, 50 * HEIGHT * 0.1, 50*HEIGHT);
    CGPoint position = CGPointMake(0, _figure * 50*HEIGHT);
    
    [_myTableView setContentOffset:position animated:YES];
    [_myTableView addSubview:_greenView];
    _greenView.backgroundColor =[UIColor greenColor];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
